package com.recsmile.rpc.provider;

import com.recsmile.rpc.bo.RpcMessageResponse;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by Administrator on 2016/8/3.
 */
public class MessageCallBack {

    private Lock lock = new ReentrantLock();
    private Condition finish = lock.newCondition();

    private RpcMessageResponse rpcMessageResponse;

    public void over(RpcMessageResponse rpcMessageResponse) {
        try{
            lock.lock();
            this.rpcMessageResponse = rpcMessageResponse;
            finish.signalAll();
        }finally {
            lock.unlock();
        }
    }


    public Object getResult() throws Exception{
        try{
            lock.lock();
            finish.await(10*1000, TimeUnit.MILLISECONDS);
            if(this.rpcMessageResponse != null){
                return rpcMessageResponse.getResult();
            }
            return null;
        }finally {
            lock.unlock();
        }
    }
}
